package ustc.browser;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

//TabAdpater handles the info from mTabControl to mTabOverview
//We extend the BaseAdapter to do a lot of custom work!
public class TabAdapter 
	extends BaseAdapter{
	//for Log info only
	private static final String TAG = "TabAdapter";
	private Context 			mContext = null;
	private LayoutInflater 		mInflater = null;
	private TabControl			mDataSet = null;
	
	//Widgets to be displayed in one row
	private class Item{
		public ImageView	mFaviIcon = null;
		public TextView		mTitle = null;
		public TextView		mURL = null;
		public ImageButton	mDeleteButton = null;
	}
	
	public TabAdapter(Context c, TabControl tc){
		mContext = c;
		mInflater = (LayoutInflater)mContext.
			getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mDataSet = tc;
	}
	
	//How many items are in the data set 
	//represented by this Adapter
	@Override
	public int getCount(){
		return	mDataSet.getTabSize();
	}
	
	//Get the data item associated with
	//the specified position
	@Override
	public Object getItem(int position) {
		return	mDataSet.getTabOfIndex(position);
	}
	
	//Get the row id associated with the specified
	//position in the data set.
	@Override
	public long getItemId(int position) {
		return	position;
	}
	
	//Get a View that displays the data
	//at the specified position in the data set
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Log.d(TAG + "getView()","position = " + position);
		Item item = null;
		final int index = position;
		if(convertView == null){
			item = new Item();
			convertView = mInflater.inflate(R.layout.listview_layout, null);
			//Initialize the elements to be displayed
			item.mFaviIcon = (ImageView)convertView.findViewById(R.id.item_image);
			item.mTitle = (TextView)convertView.findViewById(R.id.item_title);
			item.mURL = (TextView)convertView.findViewById(R.id.item_url);
			item.mDeleteButton = (ImageButton)convertView.findViewById(R.id.item_button);
			//Add the position info as the extra info
			//'item' as the extra info added to convertView
			convertView.setTag(item);
		}else{
			item = (Item)convertView.getTag();
		}
		Tab t = (Tab)getItem(position);
		if(t.getFaviIcon() != null)
			item.mFaviIcon.setImageBitmap(t.getFaviIcon());
		if(t.getTitle() != null)
			item.mTitle.setText(t.getTitle());
		if(t.getUrl() != null)
			item.mURL.setText(t.getUrl());
		//Set to handle the click event of mDeleteButton
		item.mDeleteButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(mDataSet.removeTabOfIndex(index)){
					//If mTabControl contains no Tab, ask BrowserActivity
					//to create a default webview
					if(mDataSet.getTabSize() == 0)
						((BrowserActivity)mContext).createNewWindow();
					//Notify the adapter the change of data set
					notifyDataSetChanged();
				}
			}
		});
		return	convertView;
	}
}